import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: ()=>import("@/views/admin/login.vue"),
    },
    {
      path: '/enroll',
      name: 'Enroll',
      component: ()=>import("@/views/admin/enroll.vue"),
    },
    {
      path: '/forget',
      name: 'Forget',
      component: ()=>import("@/views/admin/forget.vue"),
    },
    {
      path: '/adminIndex',
      name: 'adminIndex',
      component: ()=> import("@/views/admin/AdminIndex.vue"),
      children:[{
        path: 'notice',
        name: 'notice',
        component:()=>import('@/views/admin/AdminNotice.vue')
      },{
        path: 'goodutil',
        name: 'goodutil',
        component:()=>import('@/views/admin/AdminGoodUtil.vue')
      },{
        path: 'mngutil',
        name: 'mngutil',
        component:()=>import('@/views/admin/AdminMngUtil.vue')
      },{
        path: 'editutil',
        name: 'editutil',
        component:()=>import('@/views/admin/AdminEditUtil.vue')
      },{
        path: 'home',
        name: 'home',
        component:()=>import('@/views/admin/AdminHome.vue')
      }]
    },
    {
      path: '/:pathMatch(.*)*',
      name:"404",
      component:()=> import ('@/views/404.vue')
    },
    {
      path:'/',
      redirect: '/login'
    }
  ]
})
// 全局前置守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = !!localStorage.getItem('token'); // 假设 token 保存在 localStorage 中
  if(to.name == "Enroll" || to.name == 'Forget') {
    next();
  }
  else if (to.name !== 'Login' && !isAuthenticated) {
    next({ name: 'Login' });
  } else next();
});
export default router
